package top.codechap.ioc.framework.context.support;

import top.codechap.ioc.framework.beans.factory.support.BeanDefinitionReader;
import top.codechap.ioc.framework.beans.factory.support.BeanDefinitionRegistry;
import top.codechap.ioc.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * ApplicationContext接口的子实现类,用于立即加载
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    //声明解析器变量
    protected BeanDefinitionReader beanDefinitionReader;

    //定义用于存储bean对象的Map容器
    protected Map<String,Object> singletonObjects = new HashMap<>();

    //声明配置文件路径的变量
    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        //加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //初始化bean,即,创建bean对象
        finishBeanInitialization();
    }

    //bean的初始化
    private void finishBeanInitialization() throws Exception {
        //现获取对应的注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取BeanDefinition对象
        String[] beanNames = registry.getBeanDefinitionName();
        //进行bean的初始化操作
        for (String beanName : beanNames) {
            Object bean = getBean(beanName);
            singletonObjects.put(beanName,bean);
        }
    }
}
